package com.useeinfo.asg.open.web;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.ArticleBiz;
import com.useeinfo.asg.modules.logging.biz.CommentBiz;
import com.useeinfo.asg.modules.logging.entity.Article;
import com.useeinfo.asg.modules.logging.entity.Comment;
import com.useeinfo.asg.modules.logging.entity.FrontUser;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 */
@Controller
@RequestMapping("/mobile/comment")
public class CommentManageAction {

    @Autowired
    private ArticleBiz articleBiz;
     @Autowired
     private CommentBiz commentBiz;

    /**
     * 文章评论列表
     * @param pageNowParam
     * @param pageSizeParam
     * @param articleID
     * @return
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "articleID", required = false) String articleID) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("articleID",CommUtil.null2String(articleID));
        return articleBiz.findJSONList(queryParam);
    }

    /**
     * 提交评论
     * @param request
     * @param mv
     * @return
     */
    @RequestMapping("/commit")
    public String detail(HttpServletRequest request, ModelMap mv,String articleID,String content) {
        FrontUser user = (FrontUser)request.getSession().getAttribute("user");
        Comment comment = new Comment();
        comment.setArticle(articleBiz.findModel(CommUtil.null2Long(articleID)));
        comment.setCreateTime(new Date());
        comment.setContent(CommUtil.null2String(content));
        comment.setFrontUser(user);
        commentBiz.addOrUpdate(comment);
        mv.put("comment",comment);
        return "mobile/articleComment";
    }
}
